<%@Page Language="C#"%>
<%@ Import Namespace="System.IO" %>

<script runat=server>

	ArrayList movieList;
	
	protected void Page_Load( object sender, EventArgs e )
	{

		if ( IsPostBack == true )
		{
			movieList = (ArrayList) ViewState["movies"];
		}
		else
		{
			movieList = new ArrayList();
			ViewState["movies"] = movieList;
		}


//		if ( IsPostBack == true )
//		{
//			movieList = (ArrayList) Session["movies"];
//		}
//		else
//		{
//			movieList = new ArrayList();
//			Session["movies"] = movieList;
//		}

	}

	// Add an item to the cart 
	protected void OnAddMovie( object sender, EventArgs e )
	{
		movieList.Add( MovieName.Text );
	}
	
	// Delete an item from the cart
	protected void OnDeleteMovie( object sender, EventArgs e )
	{
		if ( movieList.IndexOf( MovieName.Text ) == -1 )
		{
			status.InnerHtml = "Movie not found in list";
			return;
		}

		movieList.Remove( MovieName.Text );
		
	}

	// Sort the list 
	protected void OnSortList( object sender, EventArgs e )
	{
		movieList.Sort();	
	}


	// Class that implements the comparison logic for the sort

	class MyComparer : IComparer
	{

		public int Compare( object x, object y )
		{
			
			IComparable ic;
			int compareResult;

			ic = (IComparable) x;
			compareResult = ic.CompareTo( y );

			if ( compareResult != 0 )
				compareResult = compareResult * -1;

			return compareResult;
		}
	}
	// Custom Sort
	protected void OnCustomSortList( object sender, EventArgs e )
	{
		IComparer custom = new MyComparer();
		movieList.Sort( custom );	
	}

	
void DoSomethingWithAList( ArrayList list )
{
	lock(list)
	{
		list.Add("abc");
	}
}


</script>

<form runat=server>

<h3>Movies in List</h3>


<%

	if ( movieList.Count != 0 ) 
	{
	
		Response.Write("<table>");
		Response.Write("<tr bgcolor=cornflowerblue>");
		Response.Write("<td>Movie Name");
		Response.Write("</tr>");

		foreach( string Movie in movieList )
		{
			Response.Write("<tr>");
			Response.Write("<td>" + Movie );
		}
		Response.Write("</table>");
	}
	else
	{
		Response.Write("<P>There are no movies in the list.");
	}
	


	ArrayList Animals = new ArrayList();
	string[] ArrayOfAnimals;

	Animals.Add( "Cat" );
	Animals.Add( "Dog" );


	ArrayOfAnimals = (string[]) Array.CreateInstance( typeof(string), 2 );
	Animals.CopyTo( ArrayOfAnimals );

	foreach( string Animal in ArrayOfAnimals )
	{
	//	Response.Write("<P>" + Animal );
	}


	string[] Friends = { "Tom","Mark" };
	string[] CoWorkers = { "Paul", "Jon" };
	ArrayList MergedList = new ArrayList();


	MergedList.AddRange( Friends );
	MergedList.InsertRange( 0, CoWorkers );

	foreach( string Name in MergedList )
	{
	//	Response.Write("<P>" + Name );
	}



%>


<table>
<tr>
	<td>Movie Name:</td>
	<td><asp:TextBox id=MovieName runat=server/>
<tr>
	<td colspan=2>
		<asp:Button OnClick="OnAddMovie" Text="Add Movie" runat=server />
		<asp:Button OnClick="OnDeleteMovie" Text="Delete Movie" runat=server />
		<asp:Button OnClick="OnSortList" Text="Sort List" runat=server />
		<asp:Button OnClick="OnCustomSortList" Text="Sort List (custom)" runat=server />
</table>

</form>

<p style="color:red" id=status EnableViewState=False runat=server />
